package generateparentheses;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hht
 * @ClassName Solution2
 * @Description TODO
 * @Date 2019/7/1 11:58
 * @VERSION 1.0
 */
public class Solution2 {
    public static void main(String[] args) {
        List list = new Solution2().generateParenthesis(3);
        System.out.println(list);
    }

    public static List generateParenthesis (int n){
        List list = new ArrayList();
        generateParenthesisByRecursive(list,"",n,0,0);
        return list;
    }

    public static void generateParenthesisByRecursive (List result,String res,int n,int left,int right){
        if(left==n && right==n){
            result.add(res);
            return;
        }
        if(left<n){
            generateParenthesisByRecursive(result,res+"(",n,left+1,right);
        }
        if(right<left){
            generateParenthesisByRecursive(result,res+")",n,left,right+1);
        }
    }



}
